package com.zbkj.crmeb.task.order;

import java.util.List;
import java.util.Objects;

import com.zbkj.crmeb.store.service.StoreOrderStatusService;
import com.zbkj.crmeb.store.utilService.OrderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.constants.Constants;
import com.crmeb.core.utils.RedisUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.crmeb.bill.service.MerFreezeBillService;
import com.zbkj.crmeb.enums.OrderStatusEnum;
import com.zbkj.crmeb.front.service.OrderService;
import com.zbkj.crmeb.store.model.OrderInfoDelivery;
import com.zbkj.crmeb.store.model.StoreOrderInfo;
import com.zbkj.crmeb.store.service.OrderInfoDeliveryService;
import com.zbkj.crmeb.store.service.OrderTaskService;
import com.zbkj.crmeb.store.service.StoreOrderInfoService;
import com.zbkj.crmeb.store.service.StoreOrderService;

import lombok.extern.slf4j.Slf4j;

@Component
@Configuration //读取配置
@Slf4j
public class OrderTakeByUser {
    //日志
    private static final Logger logger = LoggerFactory.getLogger(OrderTakeByUser.class);

    @Value("${order.signIn}")
    protected Boolean orderSignIn;

    @Autowired
    private OrderTaskService orderTaskService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private StoreOrderService storeOrderService;
    @Autowired
    private StoreOrderInfoService storeOrderInfoService;
    @Autowired
    private OrderInfoDeliveryService orderInfoDeliveryService;
    @Autowired
    private MerFreezeBillService merFreezeBillService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrderUtils orderUtils;
    @Autowired
    private StoreOrderStatusService storeOrderStatusService;

    //1分钟同步一次数据
    @XxlJob("orderTakeByUser-init")
    public void init(){
        try {
            orderTaskService.takeByUser();
            orderTaskService.takeBySystem();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("OrderTakeByUser.task" + " | msg : " + e.getMessage());
        }
    }

    /**
     * 每天凌晨订单查询确认收货超时订单
     * @Description:  
     * @author daigb 
     * @param 
     * @return void
     * @throws
     */
    @XxlJob("orderTakeByUser-batchTake")
    public void batchTake() {
        orderService.batchTake();
    }
    
    /**
     * 海关数据推送(十秒一次)
     * @Description:  
     * @author daigb 
     * @param 
     * @return void
     * @throws
     */
    @XxlJob("orderTakeByUser-customsTake")
    public void customsTake() {
    	orderService.customsTake();
    }

    /**
     * 物流签收定时任务（每天凌晨）
     */
//    @XxlJob("orderTakeByUser-logisticsSignIn")
//    public void logisticsSignIn() {
//        if (!orderSignIn) {
//            return;
//        }
//        List<StoreOrderInfo> storeOrderInfoList = storeOrderInfoService.list(Wrappers.lambdaQuery(StoreOrderInfo.class).eq(StoreOrderInfo::getStatus, OrderStatusEnum.SHIPPED.getCode()));
//        for (StoreOrderInfo storeOrderInfo : storeOrderInfoList) {
//            //查询订单终端物流信息
//            OrderInfoDelivery delivery = orderInfoDeliveryService.getOne(Wrappers.<OrderInfoDelivery>lambdaQuery().eq(OrderInfoDelivery::getOrderInfoId, storeOrderInfo.getId())
//                    .eq(OrderInfoDelivery::getLogisticsStage, 3).last("LIMIT 1"));
//            if(Objects.isNull(delivery) || !orderInfoDeliveryService.judgeSignIn(delivery)){
//                continue;
//            }
//            //修改订单状态
//            storeOrderInfo.setStatus(Constants.ORDER_STATUS_H5_COMPLETE);
//            boolean update = storeOrderInfoService.updateById(storeOrderInfo);
//            if(update){
//                log.info("---------------订单:{} 已签收超过三天,系统自动确认收货--------------------",storeOrderInfo.getChildOrderNumber());
//                storeOrderStatusService.createLog(storeOrderInfo.getOrderId(),storeOrderInfo.getChildOrderNumber(), "confirm_receipt", "物流签收三天，系统自动确认收货");
//                //修改总订单状态
//                orderUtils.updateOrderStatus(storeOrderInfo.getOrderId());
//            }
//        }
//    }
    
}
